package com.lhq.plugin.handler.translation;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.texteditor.ITextEditor;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.lhq.plugin.console.EclipseOutput;
import com.lhq.plugin.handler.BaseHandler;
import com.lhq.plugin.toolkit.AliTranslationSender;

@SuppressWarnings("deprecation")
public class TranslationHandler extends BaseHandler {
    private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
    private static final String VIEW_NAME = "org.eclipse.ui.console.ConsoleView";

    private static final String url = "http://mt.cn-hangzhou.aliyuncs.com/api/translate/web/ecommerce";
    private static final String accessKeyId;
    private static final String accessKeySecret;
    static {
        String configStr = System.getenv("SLEF_ALIYUN_ACCESS_KEY_ID");
        JsonObject config = gson.fromJson(configStr, JsonObject.class);
        accessKeyId = config.get("accessKeyId").getAsString();
        accessKeySecret = config.get("accessKeySecret").getAsString();
    }

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        ITextEditor ite = super.getTextEditor();
        TextSelection selection = super.getTextSelection(ite);
        IDocument document = super.getDocument(ite);
        String selectText = super.getSelectText(document, selection);

        if (StringUtils.isBlank(selectText)) {
            IViewPart viewPart = super.getViewPart(ite, VIEW_NAME);
            selectText = super.getSelectText(viewPart);
        }

        this.handler(selectText);
        return null;
    }

    private void handler(String selected) {
        if (StringUtils.isBlank(selected)) {
            return;
        }
        selected = selected.replaceAll("\\*", " ").replaceAll("\r", " ").replaceAll("\n", "").replace("/", " ").replaceAll(" +", " ").trim();
        if (StringUtils.isBlank(selected)) {
            return;
        }

        String result = this.transfer(selected);
        EclipseOutput.output.println(WordUtils.wrap(result, 50) + "\n");
    }
    
    public String transfer(String text) {
        try {
            JsonObject postBody = new JsonObject();
            postBody.addProperty("FormatType", "text");
            postBody.addProperty("SourceLanguage", "en");
            postBody.addProperty("TargetLanguage", "zh");
            postBody.addProperty("SourceText", text);
            postBody.addProperty("Scene", "general");
            String resultStr = AliTranslationSender.sendPost(url, postBody.toString(), accessKeyId, accessKeySecret);

            JsonObject result = gson.fromJson(resultStr, JsonObject.class);
            if (result.get("Code").getAsString().equals("200")) {
                return result.get("Data").getAsJsonObject().get("Translated").getAsString();
            } else {
                return "翻译失败: " + result.get("Message").getAsString();
            }
        } catch (Exception e) {
            return "翻译失败: " + e.getMessage();
        }
    }

}